# -*- coding: utf-8 -*-
# @FileName :conftest.py
# @Time :2024-01-31 09:57
# author: 小燕子
"""
这是固定写法
@pytest.hookimpl(hookwrapper=True,tryfirst=True)
def pytest_runtest_makereport(item,call):
    out=yield
"""
import json

import pytest
import time
RESULT = "D:/IDEProject/pythonProject06/pythonProject/pythonProject/sixth_version/result.json"
@pytest.hookimpl(hookwrapper=True,tryfirst=True)
def pytest_runtest_makereport(item,call):
    out=yield
    print("测试主体：",out)
    result = out.get_result()
    if result.when == "call": # 当result.when 是call的时候，运行的就是用例主体
        print(result)
        print(result.outcome) #测试结果
        outcome = result.outcome
        name = result.nodeid
        times = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
        print("结果",outcome)
        print("用例名",name)
        print("执行时间",times)
        f = open(RESULT,"r",encoding="utf-8")
        d = f.read()
        if d:
            result_ = json.load(d)
        else:
            result_ = {}
            f.close()
        f = open(RESULT,"w",encoding="utf-8")
        result_.update(
            {

                times:{
                    "name":name,
                    "result":outcome,
                }
            }
        )
        f.write(json.dumps(result_))
        f.close()

